home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / enigma / earcd / comm / comm1 / klrkcmsr.lha / Comment_Disc.C < prev    next >
C/C++ Source or Header  |  1995-04-11  |  12KB  |  444 lines

  1. #define KILLER_COMMENT_DISC
  2.  
  3. #include "Comment.H"
  4.  
  5. // Actions performing disc activity go here
  6.  
  7. void ReadDoorIcon ( void )
  8. {
  9.     // This routine will find the location of the command (in the right
  10.     // order and will read some tooltypes from that icon.
  11.  
  12.    char Temp[200], temp1[200], IconLoc[200];
  13.     char    *p;
  14.    int   i;
  15.  
  16.     // SYSCMD directory
  17.     if ( access( "BBS:Commands/SYSCmd/C.info" , F_OK ) )
  18.     {
  19.         // CONFxCMD directory
  20.         getuserstring( Temp , BB_CONFNUM );
  21.         i = atoi( Temp ) + 1;
  22.         sprintf( temp1 , "BBS:Commands/Conf%dCmd/C.info", i );
  23.         if ( access ( temp1 , F_OK ) )
  24.         {
  25.             // NODExCMD
  26.             getuserstring( Temp , BB_NODEID );
  27.             sprintf( temp1 , "BBS:Commands/Node%sCmd/C.info", Temp );
  28.          if ( access( temp1 , F_OK ) )
  29.             {
  30.                 // BBSCMD
  31.                 if (access( "BBS:Commands/BBSCmd/C.info" , F_OK) )
  32.                 {
  33.                     sm( "Could not find C.info in any of the command directories!\n\r" , 0 );
  34.                     enddoor ( EXIT_REGULAR_C );
  35.             }
  36.             else
  37.                strcpy(IconLoc, "BBS:Commands/BBSCmd/C");
  38.          }
  39.          else
  40.             sprintf(IconLoc, "BBS:Commands/Node%sCmd/C", Temp);
  41.       }
  42.       else
  43.          sprintf(IconLoc, "BBS:Commands/Conf%dCmd/C", i);
  44.    }
  45.    else
  46.       strcpy(IconLoc, "BBS:Commands/SYSCmd/C");
  47.  
  48.     // Open the icon and read info
  49.     if ( InitIcon( IconLoc ) )
  50.     {
  51.         // Where is our door located?
  52.         strcpy( gn_ptr->gn_doorlocation , FromIcon( "LOCATION" ) );
  53.         gn_ptr->gn_view_access = atoi( FromIcon("ACS_STATS") );
  54.         strcpy( bbs_location , FromIcon( "BBS_LOCATION" ) );
  55.         if ( FromIcon( "SEPARATOR_LINE" ) )
  56.             line_separator = TRUE;
  57.         p = FromIcon( "HEADER" );
  58.         if ( p )
  59.             strcpy( gn_ptr->gn_header , p );
  60.         else
  61.             gn_ptr->gn_header[ 0 ] = 0x00;
  62.  
  63.         // We are done with the icon, close it
  64.         CloseIcon();
  65.     }
  66.     else
  67.     {
  68.         sprintf(temp1, "Couldn't open the icon of: %s\n" , IconLoc );
  69.         sm( temp1 , 0 );
  70.         enddoor ( EXIT_REGULAR_C );
  71.     }
  72. }
  73.  
  74. void LoadPrefs ( void )
  75. {
  76.     char    Temp[ 300 ];
  77.     int    acs_level;
  78.  
  79.     struct    receiver    *dummy_ptr;
  80.     struct    USER    usr;
  81.  
  82.     FILE    *fptr, *fptr2;
  83.  
  84.     // Initialise KC_Users0List (standard procedure)
  85.     KC_Users0List.mlh_Head     = (struct Node *) &KC_Users0List.mlh_Tail;
  86.     KC_Users0List.mlh_Tail     = 0;
  87.     KC_Users0List.mlh_TailPred = (struct Node *) &KC_Users0List.mlh_Head;
  88.  
  89.     // Is there a prefs file? If so, read it. If not, fill it with default
  90.     // values.
  91.     sprintf( Temp , "%s" , gn_ptr->gn_doorlocation );
  92.     Temp[ strlen( gn_ptr->gn_doorlocation ) - strlen( FilePart ( Temp ) ) ] = 0x00;
  93.     AddPart( Temp , "KiLLER_Comment.Prefs" , 300 );
  94.     if ( ! access ( Temp , F_OK ) )
  95.     {
  96.         // Read the prefs
  97.         if ( fptr = fopen ( Temp , "r" ) )
  98.         {
  99.             sprintf( Temp, "%s" , bbs_location );
  100.             AddPart( Temp, "USER.DATA" , 200 );
  101.             if ( fptr2 = fopen ( Temp , "r" ) )
  102.             {
  103.                 // Check for correct version (and is it a config file)
  104.                 fread ( Temp , sizeof( CONFIG_VERSION ) , 1 , fptr );
  105.                 if ( strcmp ( Temp , CONFIG_VERSION ) != 0 )
  106.                 {
  107.                     sm( "Corrupt config file! Defaulting to sysop.\n\r\n\r" , 0 );
  108.                     fclose ( fptr );
  109.                     fclose ( fptr2 );
  110.                     enddoor ( EXIT_REGULAR_C );
  111.                 }
  112.                 else
  113.                 {
  114.                     // What is the sec.level of the user who is using this door?
  115.                     getuserstring( Temp , DT_SECSTATUS );
  116.                     acs_level = atoi( Temp );
  117.  
  118.                     // Read the receivers until end of file
  119.                     // In order to do so, we need to allocate memory for a dummy
  120.                     dummy_ptr = AllocVec ( sizeof ( struct receiver ) , MEMF_PUBLIC|MEMF_CLEAR );
  121.                     if ( dummy_ptr == NULL )
  122.                         enddoor( MEMORY_ERROR );
  123.     
  124.                     // Read the info about the receivers
  125.                     while ( ! feof ( fptr ) )
  126.                     {
  127.                         fread ( dummy_ptr , 1 , sizeof ( struct receiver ) , fptr );
  128.                         if ( ! feof ( fptr ) )
  129.                         {
  130.                             // Does user have access to see this user in the list?
  131.                             if ( acs_level >= dummy_ptr->rc_min_acs )
  132.                             {
  133.                                 rc_node = AllocVec ( sizeof ( struct receiver_node ) , MEMF_PUBLIC|MEMF_CLEAR );
  134.                                 if ( rc_node == NULL )
  135.                                 {
  136.                                     FreeVec( dummy_ptr );
  137.                                     fclose ( fptr2 );
  138.                                     fclose ( fptr );
  139.                                     enddoor ( MEMORY_ERROR );
  140.                                 }
  141.                                 rc_node->rc_slotnumber = dummy_ptr->rc_slotnumber;
  142.  
  143.                                 // Now that we got the user's slotnumber, we can search for his
  144.                                 // real name in the USER.DATA.
  145.                                 if ( fseek ( fptr2 , ( rc_node->rc_slotnumber - 1 ) * sizeof ( struct USER ) , SEEK_SET ) == -1 )
  146.                                 {
  147.                                     sm( "Error reading USER.DATA file for user's name. Defaulting to sysop.\n\r" , 0);
  148.                                     fclose( fptr );
  149.                                     fclose( fptr2 );
  150.                                     FreeVec ( dummy_ptr );
  151.                                     enddoor ( EXIT_REGULAR_C );
  152.                                 }
  153.                                 fread ( &usr , 1 , sizeof( struct USER ) , fptr2 );
  154.  
  155.                                 // Check of the user in the [C]omment list has got
  156.                                 // access to this conference. If not, don't put
  157.                                 // him in the list.
  158.                                 if ( UserAccess( usr.Conference_Access ) )
  159.                                 {
  160.                                     strcpy ( rc_node->rc_realname , usr.Name );
  161.  
  162.                                     // And fill the rest with the info from disk
  163.                                     gn_ptr->gn_total_users++;
  164.                                     strcpy ( rc_node->rc_knownas , dummy_ptr->rc_knownas );
  165.                                     strcpy ( rc_node->rc_info , dummy_ptr->rc_info );
  166.                                     strcpy ( rc_node->rc_comments , dummy_ptr->rc_comments );
  167.                                     rc_node->rc_msg_received = dummy_ptr->rc_msg_received;
  168.                                     rc_node->rc_last_msg = dummy_ptr->rc_last_msg;
  169.                                     rc_node->rc_last_user = dummy_ptr->rc_last_user;
  170.                                     rc_node->rc_number = gn_ptr->gn_total_users;
  171.                                     rc_node->rc_min_acs = dummy_ptr->rc_min_acs;
  172.                                     rc_node->nn_Node.ln_Name = rc_node->rc_knownas;
  173.                                     rc_node->nn_Node.ln_Type = 0;
  174.                                     rc_node->nn_Node.ln_Pri    = 0;
  175.                                     AddTail( ( struct List * ) &KC_Users0List , (struct Node *) rc_node );
  176.                                 }
  177.                             }
  178.                         }
  179.                     }
  180.                 }
  181.                 // We are done reading the file, close it, return the memory of
  182.                 // the dummy pointer and report the number of receivers.
  183.                 fclose ( fptr );
  184.                 fclose ( fptr2 );
  185.                 FreeVec ( dummy_ptr );
  186.             }
  187.             else
  188.             {
  189.                 sm("Couldn't open USER.DATA for reference.\n\r" , 0 );
  190.                 fclose( fptr );
  191.                 enddoor( EXIT_REGULAR_C );
  192.             }                
  193.         }
  194.         else
  195.         {
  196.             sm("Couldn't open preferences.\n\r" , 0 );
  197.             enddoor( EXIT_REGULAR_C );
  198.         }
  199.     }
  200.     else
  201.         sm("Could not find prefs!\n\r",0);
  202.  
  203.     // Did we have some receivers? If not, reserve memory for one receiver,
  204.     // the sysop.
  205.     if ( gn_ptr->gn_total_users == 0 )
  206.         enddoor ( EXIT_REGULAR_C );
  207.  
  208.    // Did we have ONE receiver? If so, exit gentle and write to that
  209.    // receiver.
  210.    if ( gn_ptr->gn_total_users == 1 )
  211.    {
  212.        // Set the rc_node pointer to the only user in the list
  213.        rc_node = (struct receiver_node *) KC_Users0List.mlh_Head;
  214.        enddoor( LEAVE_COMMENT );
  215.    }
  216. }
  217.  
  218. int WordCount (char *text)   /* © TyCoOn/MYSTiC */
  219. {
  220.    int l1 = 1, len, counter = 0;
  221.    BYTE temp[2];
  222.  
  223.    len = strlen(text);
  224.    temp[0] = 0x01;
  225.    while (counter <= len)
  226.    {
  227.       if ( (text[counter] == 0x20) && (temp[0] != 0x20) )
  228.          l1++;
  229.       if ( (text[counter] == 0x00) && (temp[0] == 0x20) )
  230.       {
  231.          l1--;
  232.          text[counter-1]=0x00;
  233.       }
  234.       temp[0] = text[counter];
  235.       counter++;
  236.    }
  237.    return(l1);
  238. }
  239.  
  240. ULONG InitIcon(char *IconName)
  241. {
  242.    if (dobj)
  243.       CloseIcon();
  244.    dobj = GetDiskObject(IconName);
  245.    if (!dobj)
  246.       return( FALSE );
  247.    toolarray = (char **)dobj->do_ToolTypes;
  248.    return ( TRUE );
  249. }
  250.  
  251. char *FromIcon(char *text)
  252. {
  253.    return(tooltype = FindToolType(toolarray, text));
  254. }
  255.  
  256. void CloseIcon(void)
  257. {
  258.    if (dobj)
  259.    {
  260.       FreeDiskObject(dobj);
  261.       dobj=NULL;
  262.    }
  263. }
  264.  
  265. void end (void)
  266. {
  267.   exit(0);
  268. }
  269.  
  270. void LastCommand (void)
  271. {
  272. }
  273.  
  274. void ShowComment ( void )
  275. {
  276.     long    file_size, file_pos;
  277.     char    ch;
  278.     unsigned int clock[2];
  279.     unsigned short seed[3];
  280.     FILE    *fptr;
  281.     char    temp[200], temp2[200];
  282.  
  283.     if ( ! access( rc_node->rc_comments , F_OK ) )
  284.     {        
  285.         file_size = GetFileSize( rc_node->rc_comments );
  286.  
  287.         if ( file_size != 0 )
  288.         {
  289.             timer(clock);
  290.             seed[0] = clock[0];
  291.             seed[1] = clock[1];
  292.             seed[2] = clock[0];
  293.             file_pos = nrand48( seed ) % file_size;
  294.  
  295.             if ( fptr = fopen( rc_node->rc_comments , "r" ) )
  296.             {
  297.                 // Jump to the position in the file
  298.                 fseek( fptr , file_pos , SEEK_SET );
  299.  
  300.                 // Read till we find a return (\n)
  301.                 while( ( ( ch = fgetc( fptr ) ) != '\n' ) && ( ch != EOF));
  302.  
  303.                 // If we read past the EOF, we simply jump back to the first
  304.                 // line and show it (it doesn't happen often).
  305.                 if ( ch == EOF || feof( fptr ) )
  306.                     fseek( fptr , 0 , SEEK_SET );
  307.  
  308.                 // Read the line, cut the \n at the end (if there is any) and
  309.                 // show it
  310.                 temp[ 0 ] = 0x00;
  311.                 fgets ( temp , sizeof( temp ) , fptr );
  312.                 if ( strlen( temp ) < 2 )
  313.                 {
  314.                     fseek( fptr , 0 , SEEK_SET );
  315.                     fgets( temp , sizeof( temp ) , fptr );
  316.                 }
  317.                 CUTCR ( temp );
  318.                 sprintf ( temp2 , temp , rc_node->rc_knownas );
  319.                 sm ( " " , 0 );
  320.                 sm ( temp2 , 0 );
  321.                 sm ( "\n\r\n\r" , 0 );
  322.             }
  323.         }
  324.     }
  325. }
  326.  
  327. ULONG GetFileSize (char *file)
  328. {
  329.    struct FileInfoBlock *FBlock;
  330.    BPTR   FLock;
  331.    ULONG  FSize=0L;
  332.    if ((FLock=Lock(file, ACCESS_READ))==NULL)
  333.    {
  334.       return(0L);
  335.    }
  336.    if ((FBlock=(struct FileInfoBlock *)AllocDosObject(DOS_FIB, NULL))==NULL)
  337.    {
  338.       UnLock(FLock);
  339.       return(0L);
  340.    }
  341.    if (Examine(FLock, FBlock))
  342.    {
  343.       FSize=FBlock->fib_Size;
  344.    }
  345.    UnLock(FLock);
  346.    FreeDosObject(DOS_FIB, FBlock);
  347.    return(FSize);
  348. }
  349.  
  350. void SaveNewStats ( void )
  351. {
  352.     char    Temp[ 300 ];
  353.     struct receiver *dummy_ptr;
  354.     long    file_pos;
  355.     BOOL    success = FALSE;
  356.  
  357.     FILE    *file_ptr;
  358.  
  359.     sprintf( Temp , "%s" , gn_ptr->gn_doorlocation );
  360.     Temp[ strlen( gn_ptr->gn_doorlocation ) - strlen( FilePart ( Temp ) ) ] = 0x00;
  361.     AddPart( Temp , "KiLLER_Comment.Prefs" , 300 );
  362.     if ( ! access ( Temp , F_OK ) )
  363.     {
  364.         if ( file_ptr = fopen ( Temp , "r+" ) )
  365.         {
  366.             if ( dummy_ptr = AllocVec ( sizeof( struct receiver ) , MEMF_PUBLIC|MEMF_CLEAR ) )
  367.             {
  368.                 // Check for correct version (and is it a config file)
  369.                 fread ( Temp , sizeof( CONFIG_VERSION ) , 1 , file_ptr );
  370.                 while ( ! feof ( file_ptr ) )
  371.                 {
  372.                     file_pos = ftell( file_ptr );
  373.                     fread ( dummy_ptr , 1 , sizeof( struct receiver ) , file_ptr );
  374.                     //sprintf( Temp , " FOUND: %s - %d - %s - %d\n\r" , dummy_ptr->rc_knownas , dummy_ptr->rc_msg_received , ctime( &dummy_ptr->rc_last_msg ) , dummy_ptr->rc_last_user );
  375.                     //sm( Temp , 0 );
  376.                     if ( dummy_ptr->rc_slotnumber == rc_node->rc_slotnumber )
  377.                     {
  378.                         fseek ( file_ptr , file_pos , SEEK_SET );
  379.                         dummy_ptr->rc_msg_received++;
  380.                         time(&dummy_ptr->rc_last_msg);
  381.                         getuserstring( Temp , DT_SLOTNUMBER );
  382.                         dummy_ptr->rc_last_user = atol( Temp );
  383.                         fwrite ( dummy_ptr , sizeof( struct receiver ) , 1 , file_ptr );
  384.                         success = TRUE;
  385.                         break;
  386.                     }
  387.                 }
  388.                 fclose( file_ptr );
  389.             }
  390.             FreeVec( dummy_ptr );
  391.         }
  392.     }
  393.     if ( ! success )
  394.         sm(" ERROR! Failed to write new statistics. Tell the sysop please.\n\r" , 0 );
  395. }
  396.  
  397. BOOL UserAccess( char *conf_acc )
  398. {
  399.     // This function will search the given access icon and check if
  400.     // the current conference is in it. Depending on the output it
  401.     // will either return TRUE (yes, it's in it) or FALSE.
  402.     
  403.     struct    DiskObject    *d_obj;
  404.     char        *tool_type, **tool_array;
  405.     char        temp[ 200 ];
  406.     int        i;
  407.     BOOL        rc = FALSE;
  408.  
  409.     // Is the icon.library open? It should be, but it can't hurt to
  410.     // check anyway.
  411.     if ( IconBase )
  412.     {
  413.         strcpy( temp , bbs_location );
  414.         AddPart( temp , "Access/Area." , 200 );
  415.         strcat( temp , conf_acc );
  416.         d_obj = GetDiskObject( temp );
  417.         if ( d_obj )
  418.         {
  419.             // What tooltypes are there?
  420.             tool_array = (char **) d_obj->do_ToolTypes;
  421.  
  422.             // Construct the tooltype we're looking for
  423.             getuserstring( temp , BB_CONFNUM );
  424.             i = atoi( temp ) + 1;
  425.             sprintf( temp , "CONF.%d" , i );
  426.  
  427.             // Check if the tooltype is available
  428.             if ( tool_type = FindToolType( tool_array , temp ) )
  429.                 rc = TRUE;
  430.             
  431.             // Free the object again
  432.             FreeDiskObject( d_obj );
  433.         }
  434.         else
  435.         {
  436.             sm( "Couldn't allocate ACCESS icon! Warn sysop!\n\r" , 0 );
  437.             sm( temp , 1 );
  438.         }
  439.     }
  440.     else
  441.         rc = TRUE;
  442.     return( rc );
  443. }
  444.